package com.mofang.powerdekorhelper.service;

import android.app.AlertDialog;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.mofang.powerdekorhelper.R;
import com.mofang.powerdekorhelper.activity.login.LoginActivity;
import com.mofang.powerdekorhelper.utils.ConnectionUtils;
import com.mofang.powerdekorhelper.utils.DialogUtils;
import com.mofang.powerdekorhelper.utils.L;
import com.mofang.powerdekorhelper.utils.SharePerforenceUtils;
import com.mofang.powerdekorhelper.utils.UrlTools;

/**
 * Created by luofei on 2017/12/22 0022.
 * 说明：
 */

public class DialogService extends Service {

    private Context mContext;
    private DialogUtils dialogUtils;
    private DisConnectBroadCastReceiver broadCastReceiver;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        broadCastReceiver = new DisConnectBroadCastReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("DisConnection");
        registerReceiver(broadCastReceiver, filter);

        return super.onStartCommand(intent, flags, startId);
    }

    // 下线广播
    class DisConnectBroadCastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            mContext = context;
            disConnect(context);
        }
    }

    private boolean ispDialogShow;
    AlertDialog alert;

    private void disConnect(Context context) {

        L.i("DisConnect", "exit");
        dialogUtils = DialogUtils.getInstance();
        if (!ispDialogShow) {
            L.i("DisConnect", "disConnect------------>");
            ispDialogShow = true;
            if (mContext != null) {
                L.i("DisConnect", "DialogShow------------>");
                View view = LayoutInflater.from(mContext).inflate(R.layout.disconect_dialog_layout, null);
                view.findViewById(R.id.disconnect_dialog_exit_tv).setOnClickListener(new MyDialogCancleListener());
                view.findViewById(R.id.disconnect_dialog_relogin_tv).setOnClickListener(new MyDialogSureListener());
                AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
                try {
                    alert = builder.create();
                    alert.requestWindowFeature(Window.FEATURE_NO_TITLE);
                    alert.getWindow().setType(WindowManager.LayoutParams.TYPE_TOAST);
                    alert.setCancelable(false);
                    alert.show();
                    alert.setContentView(view);
                } catch (Exception e) {
                    Toast.makeText(this, "您的账号在其他设备登录，请重新登录", Toast.LENGTH_SHORT).show();
                    reLogin();
                }
            }
        }
    }

    private class MyDialogSureListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            Integer userId = SharePerforenceUtils.getInstance(mContext).getUserID();
            ConnectionUtils connectionUtils = ConnectionUtils.getInstance();
            connectionUtils.init(mContext, UrlTools.WSUrl + userId);
            connectionUtils.connect();
            ispDialogShow = false;
            alert.dismiss();
        }
    }

    private class MyDialogCancleListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            reLogin();
        }
    }

    private void reLogin() {
        SharePerforenceUtils.getInstance(mContext).setLogin(false);
        ispDialogShow = false;
        Intent intent = new Intent(mContext, LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("enterFlag", 1);
        startActivity(intent);
        alert.dismiss();
        // mContext.finis
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        L.i("DialogService", "onDestroy----------------->");
        if (broadCastReceiver != null) {
            unregisterReceiver(broadCastReceiver);
        }
    }

    @Override
    public boolean stopService(Intent name) {
        L.i("DialogService", "stopService----------------->");
        return super.stopService(name);
    }
}
